'''
Created on Feb 25, 2011

'''
import FrameController
import RevisionHistory
import StyloInterface
import FeatureHandler
import ProjectWizard
import tkFileDialog
import tkMessageBox
import FileHandler
import os

class Anonymouth(object):
    '''
    The main driver for the entire application. It holds the primary data structures for the user as well as 
    contains the most basic functions which will let the user subsequently access all of the programs features and capabilities
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self.document = ""
        self.currentMatch = ''
        self.currentAuthor = ''
        self.featureSuggestionList = []
        self.revisionHistoryList = []
        self.styloInterface = StyloInterface.StyloInterface()
        self.fileHandler = FileHandler.FileHandler(self.styloInterface)
        
	def getCorpus(self):
		return self.corpusPath
	
	def setCorpus(self, corpus):
		self.corpusPath = corpus
	
    def save(self):
        pass
    
    def load(self):
        pass
    
    def setFileHandler(self,fileHandler):
        self.fileHandler = fileHandler
    
    '''Create a new project with the ProjectWizard'''
    def newProject(self,frame,masterFrame,frameController):
        projectWizard = ProjectWizard.ProjectWizard(frame, masterFrame,frameController)
        projectWizard.run(0)
    
    def selectCorpus(self, corpusPaths):
        pass
    
    '''Train the corpus with "username-project title" in the corpoa folder'''
    def trainCorpus(self):
        self.styloInterface.trainCorpus(self.username + '-' + self.projectTitle)
    
    '''Submit the document and return the list of suggestions for each feature'''
    def submit(self,revision, timestamp):
        self.addRevision(revision,timestamp)
        features = self.styloInterface.submit(revision,self.username + '-' + self.projectTitle)
        featureHandler = FeatureHandler.FeatureHandler()
        self.featureSuggestionList = featureHandler.createSuggestion(features, revision)
        self.currentMatch = featureHandler.currentMatch
        self.currentAuthor = featureHandler.currentAuthor
        return self.featureSuggestionList
		
    def getCurrentMatchValue(self):
        return self.currentMatch
    
    '''
    Functions relating to RevisionHistory
    '''
    
    def addRevision(self,document,timestamp):
        revision=RevisionHistory.RevisionHistory(document,timestamp)
        self.revisionHistoryList.append(revision)
        return revision
        
    def getRevisionFromIndex(self,revisionIndex):
        return self.revisionHistoryList[revisionIndex]
        
    def popRevision(self):
        return self.popRevisionFromIndex(self.revisionHistoryList[self.revisionHistoryList.count()])
    
    def popRevisionFromIndex(self,revisionIndex):
        revision = self.getRevisionFromIndex(revisionIndex)
        del self.revisionHistoryList[revisionIndex:]
        return revision
    
    
    '''
    Functions relating to saving and opening
    '''
    
    def saveProject(self,frame, promptForFilename):
        self.fileHandler.clearProjectSettings()
        self.fileHandler.writeProjectSetting('user',self.username)
        self.fileHandler.writeProjectSetting('title',self.projectTitle)
        self.fileHandler.writeProjectSetting('documentFilename',self.documentFilename)
        self.fileHandler.writeProjectSetting('corpusPath',self.corpusPath)
        self.fileHandler.saveProject(self.projectFilename)
        self.fileHandler.writeText(self.documentFilename,frame.getDocumentText())
    
    def openProject(self,frameController,projectFilename=None):
        if projectFilename is None:
            projectFilename = frameController.promptOpenFile()
            if projectFilename is None:
                return None
        self.projectFilename = projectFilename
        self.fileHandler.loadProject(projectFilename)
        self.username = self.fileHandler.getProjectSetting('user')
        self.projectTitle = self.fileHandler.getProjectSetting('title')
        self.documentFilename = self.fileHandler.getExistingFile(
                                                                 frameController,
                                                                 self.fileHandler.getProjectSetting('documentFilename'),
                                                                 self.fileHandler.getPathFromFilename(projectFilename)
                                                                 )
        self.corpusPath = self.fileHandler.getExistingFolder(
                                                             frameController,
                                                             self.fileHandler.getProjectSetting('corpusPath'),
                                                             self.fileHandler.getPathFromFilename(projectFilename)
                                                             )
        frameController.setTextArea(self.fileHandler.readText(self.documentFilename))
    
    def getExistingFolder(self,pathname,initialDirectory=None):
        if initialDirectory is None:
            initialDirectory = self.fileHandler.getCWD()
            
    def getCorpusPaths(self):
        return self.fileHandler.getSubfolders(self.corpusPath,False,True)
    
    def removeCorpusFolder(self,foldername):
        return self.fileHandler.rmdir(self.corpusPath+"/"+foldername)
    
    def addCorpusFolder(self,pathName):
        return self.fileHandler.copyFolder(pathName,self.corpusPath+"/"+os.path.basename(pathName))
    
if __name__ == '__main__': 
    driver = Anonymouth()
    controller = FrameController.FrameController(driver)
        